package com.example.projectcode.controller.Student;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.projectcode.model.Student;
import com.example.projectcode.result.Result;
import com.example.projectcode.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "学生管理接口")
@RestController
@RequestMapping(value = "/user/administrateStudent")
public class AdministrateStudentController {
    @Autowired
    private StudentService studentService;

     //新增学生
    @ApiOperation(value = "新增学生")
    @PostMapping("/insert")
    public Result insertStudent(@RequestBody Student student){
        boolean result = studentService.insertStudent(student);
         if (result){
             return Result.ok();
         }else {
             return Result.fail();
         }

    }


    @ApiOperation(value = "根据学生id删除学生")
    @DeleteMapping("/delete/{id}")
    public Result deleteStudent(@PathVariable Integer id){
        int result = studentService.deleteStudentById(id);
        if (result>0){
            return Result.ok();

        }else {
            return Result.fail();
        }

    }

    @PostMapping("/update")
    @ApiOperation(value = "根据学生id修改学生信息")
    public Result updateStudent(@RequestBody Student student){
        int result =  studentService.updateStudent(student);
        if (result>0){
            return Result.ok();
        }
        else {
            return Result.fail();
        }
    }

    @GetMapping("/queryAllStudents/{page}/{size}")
    @ApiOperation(value = "分页查询所有学生")
    public Result queryAllStudents(@PathVariable long page,@PathVariable long size){
        Page<Student> page1 = new Page<>(page,size);
        IPage<Student> iPage = studentService.page(page1,null);
        return Result.ok(iPage);
    }





}
